package com.jade.editor.anisotropy.node;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class NodeSettings extends Dialog {

	private int _x = 0;
	private int _y = 0;
	private int _xMax = 100;
	private int _yMax = 100;
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public NodeSettings(Shell parentShell, int x, int y, Point size) {
		super(parentShell);
		_x = x;
		_y = y;
		_xMax = size.x;
		_yMax = size.y;
	}
	
	public int getX(){
		return _x;
	}
	
	public int getY(){
		return _y;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(null);
		
		final Spinner xSpinner = new Spinner(container, SWT.BORDER);
		xSpinner.setMaximum(_xMax);
		xSpinner.setMinimum(0);
		xSpinner.setSelection(_x);
		xSpinner.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				_x = xSpinner.getSelection();
			}
		});
		xSpinner.setBounds(25, 10, 75, 22);
		
		final Spinner ySpinner = new Spinner(container, SWT.BORDER);
		ySpinner.setMaximum(_yMax);
		ySpinner.setMinimum(0);
		ySpinner.setSelection(_y);
		ySpinner.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				_y = ySpinner.getSelection();
			}
		});
		ySpinner.setBounds(140, 10, 75, 22);
		
		Label lblX = new Label(container, SWT.NONE);
		lblX.setBounds(10, 17, 55, 15);
		lblX.setText("X:");
		
		Label lblY = new Label(container, SWT.NONE);
		lblY.setBounds(122, 17, 55, 15);
		lblY.setText("Y:");

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(233, 121);
	}
}
